// 设置文件
import setting from '@/setting.js'

const state = {
  // 顶栏菜单
  header: [],
  // 侧栏菜单
  aside: [],
  // 侧边栏收缩
  asideCollapse: setting.menu.asideCollapse
}

const mutations = {
  asideCollapseSet (state, asideCollapse) {
    state.asideCollapse = asideCollapse
  },
  /**
   * @description 设置顶栏菜单
   * @param {Object} state vuex state
   * @param {Array} menu menu setting
   */
  headerSet (state, menu) {
    // store 赋值
    state.header = menu
  },
  /**
   * @description 设置侧边栏菜单
   * @param {Object} state vuex state
   * @param {Array} menu menu setting
   */
  asideSet (state, menu) {
    // store 赋值
    state.aside = menu
  }
}

const actions = {
  /**
   * 设置侧边栏展开或者收缩
   * @param {Object} state vuex state
   * @param {Boolean} collapse is collapse
   */
  asideCollapseSet ({ state, commit, dispatch }, collapse) {
    return new Promise(async resolve => {
      // store 赋值
      // state.asideCollapse = collapse
      commit('asideCollapseSet', collapse)
      // 持久化
      /* await dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'menu.asideCollapse',
        value: state.asideCollapse,
        user: true
      }, { root: true }) */
      // end
      resolve()
    })
  },
  /**
   * 切换侧边栏展开和收缩
   * @param {Object} state vuex state
   */
  asideCollapseToggle ({ state, commit, dispatch }) {
    return new Promise(async resolve => {
      // store 赋值
      // state.asideCollapse = !state.asideCollapse
      commit('asideCollapseSet', !state.asideCollapse)
      // 持久化
      /* await dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'menu.asideCollapse',
        value: state.asideCollapse,
        user: true
      }, { root: true }) */
      // end
      resolve()
    })
  },
  /**
   * 从持久化数据读取侧边栏展开或者收缩
   * @param {Object} state vuex state
   */
  asideCollapseLoad ({ state, dispatch }) {
    return new Promise(async resolve => {
      // store 赋值
      state.asideCollapse = await dispatch('d2admin/db/get', {
        dbName: 'sys',
        path: 'menu.asideCollapse',
        defaultValue: setting.menu.asideCollapse,
        user: true
      }, { root: true })
      // end
      resolve()
    })
  }
}

const getters = {
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
